package com.liuyucheng.designpattern.观察者模式;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 被观察者,主体
 */
public class Subject {

    private final List<Observer> observers = new ArrayList<Observer>();

    private BigDecimal cash;

    public void setCash(BigDecimal cash) {
        this.cash = cash;
        notifyAllObservers();
    }

    public BigDecimal getCash() {
        return cash;
    }

    /**
     * 添加新的观察者
     * @param observer 观察者
     */
    public void attach(Observer observer){
        observers.add(observer);
    }

    public void notifyAllObservers(){
        for (Observer observer : observers) {
            observer.notice();
        }
    }
}